/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditTimeSlotDialog.java
 *
 * Created on Feb 15, 2012, 3:43:26 PM
 */
package ui.windows;

import entities.Shift;
import java.awt.Color;
import java.sql.Time;
import javax.swing.JOptionPane;
import ui.panels.TimeSlotManagementPanel;

/**
 *
 * @author viev
 * EditTimeSlotDialog used to show dialog to edit time slot.
 */
public class EditTimeSlotDialog extends javax.swing.JDialog {

    /** Creates new form EditTimeSlotDialog */
    public EditTimeSlotDialog(RemedySchedulerFrame mainFrame, TimeSlotManagementPanel parentPanel) {
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        initComponents();
        includeModel();
    }
    
    /**
     * 
     * @param record 
     * set selected record of time slot.
     */
    public void setSelectedRecord(Shift record) {
        selectedRecord = record;
        if(selectedRecord != null) {
            lbID.setText(selectedRecord.getShiftID()+"");
            lbName.setText(selectedRecord.getName());
            Time startTime = selectedRecord.getStartTime();
            Time endTime = selectedRecord.getEndTime();
            txtStartHour.setText(startTime.getHours()+"");
            txtMinute1.setText(startTime.getMinutes()+"");
            txtEndHour.setText(endTime.getHours()+"");
            txtMinute2.setText(endTime.getMinutes()+"");
        }
    }
    
    /**
     * include model
     */
    private void includeModel() {        
        lbCheckTime1.setText("");
        lbCheckTime2.setText("");
        lbCheckTime.setText("");
        checkStartTime = false;
        checkEndTime = false;
        checkTime = false;
    }

    /**
     * update shift action.
     */
    private void updateShift() {
        checkStartTime();
        checkEndTime();
        checkTime();
        if(checkStartTime && checkEndTime && checkTime) {
            Shift record = new Shift();
            record.setShiftID(selectedRecord.getShiftID());
            record.setName(selectedRecord.getName());
            record.setStartTime(new Time(Integer.parseInt(txtStartHour.getText()), 
                    Integer.parseInt(txtMinute1.getText()), 0));
            record.setEndTime(new Time(Integer.parseInt(txtEndHour.getText()), 
                    Integer.parseInt(txtMinute2.getText()), 0));
            boolean returnVal = parentPanel.updateShift(record);
            if(returnVal) {
                close();
                JOptionPane.showMessageDialog(mainFrame, "Updated");
            } else {
                JOptionPane.showMessageDialog(mainFrame, "Cannot update !", "Error", JOptionPane.WARNING_MESSAGE);
            }
        }
    }
    
    /**
     * check start time.
     */
    private void checkStartTime() {
        String hour = txtStartHour.getText();
        if (hour != null && hour.length() != 0) {
            int startHour;
            try {
                startHour = Integer.parseInt(hour);
                if (startHour < 0 || startHour > 23) {
                    throw new Exception();
                }
                String minute1s = txtMinute1.getText();
                if (minute1s != null && minute1s.length() != 0) {
                    int minute1;
                    try {
                        minute1 = Integer.parseInt(minute1s);
                        if (minute1 < 0 || minute1 > 59) {
                            throw new Exception();
                        }
                        lbCheckTime1.setText("valid");
                        lbCheckTime1.setForeground(new Color(102, 102, 102));
                        checkStartTime = true;
                    } catch (Exception e) {
                        lbCheckTime1.setText("invalid !");
                        lbCheckTime1.setForeground(Color.red);
                        checkStartTime = false;
                    }
                } else {
                    lbCheckTime1.setText("enter minute !");
                    lbCheckTime1.setForeground(Color.blue);
                    checkStartTime = false;
                }
            } catch (Exception e) {
                lbCheckTime1.setText("invalid !");
                lbCheckTime1.setForeground(Color.red);
                checkStartTime = false;
            }
        } else {
            lbCheckTime1.setText("enter hour !");
            lbCheckTime1.setForeground(Color.blue);
            checkStartTime = false;
        }
    }

    /**
     * check end time
     */
    private void checkEndTime() {
        String hour = txtEndHour.getText();
        if (hour != null && hour.length() != 0) {
            int endHour;
            try {
                endHour = Integer.parseInt(hour);
                if (endHour < 0 || endHour > 23) {
                    throw new Exception();
                }
                String minute2s = txtMinute2.getText();
                if (minute2s != null && minute2s.length() != 0) {
                    int minute2;
                    try {
                        minute2 = Integer.parseInt(minute2s);
                        if (minute2 < 0 || minute2 > 59) {
                            throw new Exception();
                        }
                        lbCheckTime2.setText("valid");
                        lbCheckTime2.setForeground(new Color(102, 102, 102));
                        checkEndTime = true;
                    } catch (Exception e) {
                        lbCheckTime2.setText("invalid !");
                        lbCheckTime2.setForeground(Color.red);
                        checkEndTime = false;
                    }
                } else {
                    lbCheckTime2.setText("enter minute !");
                    lbCheckTime2.setForeground(Color.blue);
                    checkEndTime = false;
                }
            } catch (Exception e) {
                lbCheckTime2.setText("invalid !");
                lbCheckTime2.setForeground(Color.red);
                checkEndTime = false;
            }
        } else {
            lbCheckTime2.setText("enter hour !");
            lbCheckTime2.setForeground(Color.blue);
            checkEndTime = false;
        }
    }
    
    /**
     * Check valid time, about value of start time and end time
     */
    private void checkTime() {
        try {
            int startHour = Integer.parseInt(txtStartHour.getText());
            int endHour = Integer.parseInt(txtEndHour.getText());
            int minute1 = Integer.parseInt(txtMinute1.getText());
            int minite2 = Integer.parseInt(txtMinute2.getText());
            int startTime = startHour*60 + minute1;
            int endTime = endHour*60 + minite2;
            if(startTime < endTime) {
                checkTime = true;
                lbCheckTime.setText("");
            } else {
                checkTime = false;
                lbCheckTime.setText("time invalid !");
                lbCheckTime.setForeground(Color.red);
            }
        } catch (Exception e) {
            checkTime = false;
        }
    }

    /**
     * close dialog
     */
    private void close() {
        this.dispose();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        bClose = new javax.swing.JButton();
        bUpdate = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbID = new javax.swing.JLabel();
        lbName = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtStartHour = new javax.swing.JTextField();
        txtEndHour = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtMinute1 = new javax.swing.JTextField();
        txtMinute2 = new javax.swing.JTextField();
        lbCheckTime1 = new javax.swing.JLabel();
        lbCheckTime2 = new javax.swing.JLabel();
        lbCheckTime = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Time Slot");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        bClose.setText("Close");
        bClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCloseActionPerformed(evt);
            }
        });

        bUpdate.setText("Update");
        bUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(299, Short.MAX_VALUE)
                .addComponent(bUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bClose, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bClose)
                    .addComponent(bUpdate))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Agency FB", 1, 18)); // NOI18N
        jLabel1.setText("Edit Time Slot");

        jLabel2.setText("ID:");

        jLabel3.setText("Name:");

        jLabel4.setText("Start Time:");

        jLabel5.setText("End Time:");

        lbID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbID.setText("value");

        lbName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbName.setText("value");

        jLabel8.setForeground(new java.awt.Color(102, 102, 102));
        jLabel8.setText("hour:");

        jLabel9.setForeground(new java.awt.Color(102, 102, 102));
        jLabel9.setText("hour:");

        txtStartHour.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtStartHourCaretUpdate(evt);
            }
        });

        txtEndHour.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtEndHourCaretUpdate(evt);
            }
        });

        jLabel10.setForeground(new java.awt.Color(102, 102, 102));
        jLabel10.setText("minute:");

        jLabel11.setForeground(new java.awt.Color(102, 102, 102));
        jLabel11.setText("minute:");

        txtMinute1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtMinute1CaretUpdate(evt);
            }
        });

        txtMinute2.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtMinute2CaretUpdate(evt);
            }
        });

        lbCheckTime1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckTime1.setText("value");

        lbCheckTime2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckTime2.setText("value");

        lbCheckTime.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckTime.setText("value");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(43, 43, 43)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbCheckTime, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtEndHour))
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtStartHour, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(lbName)
                                    .addComponent(lbID))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel11)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtMinute2))
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtMinute1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbCheckTime2)
                                    .addComponent(lbCheckTime1))))))
                .addGap(108, 108, 108))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(28, 28, 28)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel8)
                    .addComponent(txtStartHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtMinute1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckTime1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel9)
                    .addComponent(txtEndHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(txtMinute2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckTime2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbCheckTime)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtStartHourCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtStartHourCaretUpdate
    checkStartTime();
}//GEN-LAST:event_txtStartHourCaretUpdate

private void txtMinute1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtMinute1CaretUpdate
    checkStartTime();
}//GEN-LAST:event_txtMinute1CaretUpdate

private void txtEndHourCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtEndHourCaretUpdate
    checkEndTime();
}//GEN-LAST:event_txtEndHourCaretUpdate

private void txtMinute2CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtMinute2CaretUpdate
    checkEndTime();
}//GEN-LAST:event_txtMinute2CaretUpdate

private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpdateActionPerformed
    updateShift();
}//GEN-LAST:event_bUpdateActionPerformed

private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCloseActionPerformed
    close();
}//GEN-LAST:event_bCloseActionPerformed

    private RemedySchedulerFrame mainFrame;
    private TimeSlotManagementPanel parentPanel;
    private boolean checkStartTime, checkEndTime, checkTime;
    private Shift selectedRecord;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bClose;
    private javax.swing.JButton bUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lbCheckTime;
    private javax.swing.JLabel lbCheckTime1;
    private javax.swing.JLabel lbCheckTime2;
    private javax.swing.JLabel lbID;
    private javax.swing.JLabel lbName;
    private javax.swing.JTextField txtEndHour;
    private javax.swing.JTextField txtMinute1;
    private javax.swing.JTextField txtMinute2;
    private javax.swing.JTextField txtStartHour;
    // End of variables declaration//GEN-END:variables
}
